package com.demo.tests.cases;

import com.demo.config.ApiConfig;
import com.demo.config.ApiEndpoints;
import com.demo.config.ApiHeaders;
import com.demo.config.DatabaseConfig;
import com.demo.tests.base.BaseTest;
import com.demo.utils.*;
import io.qameta.allure.*;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.apache.logging.log4j.Logger;
import org.testng.annotations.*;
import org.testng.asserts.SoftAssert;

import java.sql.*;
import java.util.*;

@Feature("API测试套件")
@Story("接口测试")
public class DemoTest extends BaseTest { // 继承 BaseTest，确保登录逻辑生效
    private static final Logger log = LogUtils.getLogger();

    // 测试用例数据模型
    public static class TestCase {
        private String name;
        private Map<String, Object> parameters;
        private Map<String, Object> expected;

        public String getName() { return name; }
        public Map<String, Object> getParameters() { return parameters; }
        public Map<String, Object> getExpected() { return expected; }
    }

    @BeforeClass
    public void setUp() {
        log.info("===== 开始初始化测试配置 =====");
    }
    @DataProvider(name = "yamlDataProvider")
    public Object[][] yamlDataProvider() {
        try {
            List<TestCase> testCases = YamlUtils.loadYamlListFromClasspath(
                    "testdata/richtext_testdata.yaml",
                    TestCase.class);
            return testCases.stream()
                    .map(testCase -> new Object[]{testCase})
                    .toArray(Object[][]::new);
        } catch (Exception e) {
            log.error("加载测试数据失败", e);
            throw new RuntimeException("加载测试数据失败", e);
        }
    }

    @Test(groups = "smokedata", dataProvider = "yamlDataProvider",retryAnalyzer = RetryAnalyzer.class)
    @Severity(SeverityLevel.CRITICAL)
    public void testPostRichtext(TestCase testCase) {
        SoftAssert softAssert = new SoftAssert();
        String testCaseName = testCase.getName();

        // 从测试用例中获取参数
        int pageIndex = (int) testCase.getParameters().get("pageIndex");
        int pageSize = (int) testCase.getParameters().get("pageSize");
        String sort = (String) testCase.getParameters().get("sort");
        String wheres = (String) testCase.getParameters().get("wheres");
        int expectedStatusCode = (int) testCase.getExpected().get("statusCode");

        log.info("【开始测试】用例: {}, 参数: pageIndex={}, pageSize={}, sort={}, wheres={}",
                testCaseName, pageIndex, pageSize, sort, wheres);

        // 1. 执行API请求
        String requestBody = String.format("{\"pageIndex\":%d,\"pageSize\":%d,\"sort\":\"%s\",\"wheres\":\"%s\"}",
                pageIndex, pageSize, sort, wheres);

        Response response = RestAssured.given()
                .header("token", ApiHeaders.TOKEN)
                .header("Uid", "1")
                .header("User-Agent", ApiHeaders.USER_AGENT)
                .body(requestBody)
                .post(ApiEndpoints.getFullUrl(ApiEndpoints.RICHTEXT_GET));

//        // 2. 验证API响应
//        softAssert.assertEquals(response.statusCode(), expectedStatusCode,
//                String.format("【用例 %s】HTTP状态码不匹配，期望: %d，实际: %d",
//                        testCaseName, expectedStatusCode, response.statusCode()));

        Allure.addAttachment("响应体", response.asPrettyString());
        log.info("API响应:\n{}", response.asPrettyString());


//        softAssert.assertAll();
    }
}